using System.Collections.Generic;
using UnityEngine;

public class BotBattleRoyale : BotHoleController
{
	public Hole targetHole;

	public float agressiveTimer;

	public static int nbTargetPlayer;

	private bool _firstTarget = true;

	private float _timeStart;

	protected override void Awake()
	{
		base.Awake();
		_sensitivity = 10f;
		_timeStart = Time.time;
	}

	private void Update()
	{
		if (!BaseGameManager.Instance.HasGameStarted || BaseGameManager.Instance._hasGameEnded)
		{
			return;
		}
		if (_hole.IsDead)
		{
			if (targetHole != null && targetHole == BaseGameManager.Instance.CurrentHole)
			{
				nbTargetPlayer--;
			}
			targetHole = null;
			return;
		}
		if ((bool)targetObject && targetObject.IsDestroyed)
		{
			target = null;
		}
		if ((bool)targetHole)
		{
			if (targetHole.IsDead || targetHole.Size >= _hole.Size)
			{
				if (targetHole == BaseGameManager.Instance.CurrentHole)
				{
					nbTargetPlayer--;
				}
				targetHole = null;
				return;
			}
			Vector3 vector = Vector3.MoveTowards(_cachedTransform.position, targetHole.transform.position, Time.deltaTime * _hole.Speed * _sensitivity);
			Vector3 position = _cachedTransform.position;
			vector.y = position.y;
			SetDirection(vector - _cachedTransform.position);
			_cachedTransform.position = vector;
			if (changeTargetTimer - Time.time < 0f || _cachedTransform.position == targetHole.transform.position)
			{
				if (targetHole == BaseGameManager.Instance.CurrentHole)
				{
					nbTargetPlayer--;
				}
				targetHole = null;
			}
			return;
		}
		if ((bool)target)
		{
			if (!DestroyTargetIfOutOfBounds())
			{
				Vector3 vector2 = Vector3.MoveTowards(_cachedTransform.position, target.position, Time.deltaTime * _hole.Speed * _sensitivity);
				Vector3 position2 = _cachedTransform.position;
				vector2.y = position2.y;
				SetDirection(vector2 - _cachedTransform.position);
				_cachedTransform.position = vector2;
				if (changeTargetTimer - Time.time < 0f || _cachedTransform.position == target.position)
				{
					target = null;
				}
			}
			return;
		}
		SetTargetHole();
		if (_firstTarget && Random.Range(0f, 1f) < 0.5f)
		{
			target = FindFurthestTarget();
			if (target != null)
			{
				targetObject = target.GetComponent<FallingObjectBehaviour>();
				changeTargetTimer = Time.time + Random.Range(10f, 15f);
			}
		}
		else
		{
			target = FindClosestTarget();
			if (target != null)
			{
				targetObject = target.GetComponent<FallingObjectBehaviour>();
				changeTargetTimer = Time.time + Random.Range(5f, 10f);
			}
		}
		_firstTarget = false;
	}

	public float GetAggressivityRatio()
	{
		float num = Time.time - _timeStart;
		return Mathf.Min(1f, num / 90f);
	}

	private void SetTargetHole()
	{
		if (targetHole != null && targetHole == BaseGameManager.Instance.CurrentHole)
		{
			nbTargetPlayer--;
		}
		targetHole = null;
		float aggressivityRatio = GetAggressivityRatio();
		if (agressiveTimer == 0f)
		{
			agressiveTimer = Time.time + Random.Range(2.5f, 10f);
		}
		else if (!(agressiveTimer - Time.time > 0f))
		{
			agressiveTimer = 0f;
			float min = Mathf.Lerp(200f, 20000f, aggressivityRatio);
			float max = Mathf.Lerp(500f, 5000000f, aggressivityRatio);
			targetHole = FindClosestHole(Random.Range(0f, 1f) > aggressivityRatio, Random.Range(min, max));
			if (targetHole == BaseGameManager.Instance.CurrentHole)
			{
				nbTargetPlayer++;
				changeTargetTimer = Time.time + Mathf.Lerp(5f, 30f, aggressivityRatio);
			}
			else
			{
				changeTargetTimer = Time.time + Random.Range(3f, 8f);
			}
		}
	}

	private Hole FindClosestHole(bool targetPlayersPriorty = false, float distanceMax = 0f)
	{
		List<Hole> currentHoles = BaseGameManager.Instance.CurrentHoles;
		if (nbTargetPlayer >= 2)
		{
			targetPlayersPriorty = false;
		}
		Hole hole = null;
		foreach (Hole item in currentHoles)
		{
			if (!(item == null) && !(item == _hole) && !(item.Size >= _hole.Size) && !item.IsDead && (!(distanceMax > 0f) || !((item.transform.position - _cachedTransform.position).sqrMagnitude > distanceMax)) && (hole == null || (item.transform.position - _cachedTransform.position).sqrMagnitude < (hole.transform.position - _cachedTransform.position).sqrMagnitude))
			{
				hole = item;
				if (targetPlayersPriorty && !hole.IsBot)
				{
					break;
				}
			}
		}
		if (hole == null)
		{
			return null;
		}
		return hole;
	}
}
